package com.animee.jiujiangdict.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.telephony.mbms.MbmsErrors;
import android.text.TextUtils;
import android.util.Log;

import com.animee.jiujiangdict.bean.ChengyuBean;
import com.animee.jiujiangdict.bean.PinyinWordBean;
import com.animee.jiujiangdict.bean.WordBean;
import java.util.ArrayList;
import java.util.List;
/*
* 数据库的管理工具类
* */
public class DBManager {
    private static SQLiteDatabase db;
    public static void initDB(Context context){
        DBOpenHelper helper = new DBOpenHelper(context);
        db = helper.getWritableDatabase();
    }
    /**执行插入数据到pywordtb表当中
     * 插入一个对象的方法
     * */
    public static void insertWordToPywordtb(PinyinWordBean.ResultBean.ListBean bean){
        ContentValues values = new ContentValues();
        values.put("id",bean.getId());
        values.put("zi",bean.getZi());
        values.put("py",bean.getPy());
        values.put("wubi",bean.getWubi());
        values.put("pinyin",bean.getPinyin());
        values.put("bushou",bean.getBushou());
        values.put("bihua",bean.getBihua());
        db.insert("pywordtb",null,values);
    }
    /** 直接插入很多数据到pywordtb表当中
     * 插入多个对象的方法
     * */
    public static void insertListToPywordtb(List<PinyinWordBean.ResultBean.ListBean>list){
        if (list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                PinyinWordBean.ResultBean.ListBean bean = list.get(i);  //获取集合当中的每一个对象
                try {
                    insertWordToPywordtb(bean);
                }catch (Exception e){
                    Log.i("lsh", "insertListToPywordtb: bean"+bean.getZi()+"已存在！");
                }
            }
        }
    }
    /*
    * 查询pywordtb表当中指定拼音的数据
    * */
    public static List<PinyinWordBean.ResultBean.ListBean> queryPyWordFromPywordtb(String pinyin, int page, int pagesize){
        List<PinyinWordBean.ResultBean.ListBean>list = new ArrayList<>();
//        0,48   1    48,48+48    (i-1)*pagesize   i*pagesize
        String sql = "select * from pywordtb where py=? limit ?,?";
        int start = (page-1)*pagesize;
        int end = page*pagesize;
        Cursor cursor = db.rawQuery(sql, new String[]{pinyin, start + "", end + ""});
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String zi = cursor.getString(cursor.getColumnIndex("zi"));
            String py = cursor.getString(cursor.getColumnIndex("py"));
            String wubi = cursor.getString(cursor.getColumnIndex("wubi"));
            String pinyin1 = cursor.getString(cursor.getColumnIndex("pinyin"));
            String bushou = cursor.getString(cursor.getColumnIndex("bushou"));
            String bihua = cursor.getString(cursor.getColumnIndex("bihua"));
            PinyinWordBean.ResultBean.ListBean bean = new PinyinWordBean.ResultBean.ListBean(id, zi, py, wubi, pinyin1, bushou, bihua);
            list.add(bean);
        }
        return list;
    }

    /*
     * 查询pywordtb表当中指定拼音的数据
     * */
    public static List<PinyinWordBean.ResultBean.ListBean>queryBsWordFromPywordtb(String bushou,int page,int pagesize){
        List<PinyinWordBean.ResultBean.ListBean>list = new ArrayList<>();
//        0,48   1    48,48+48    (i-1)*pagesize   i*pagesize
        String sql = "select * from pywordtb where bushou=? limit ?,?";
        int start = (page-1)*pagesize;
        int end = page*pagesize;
        Cursor cursor = db.rawQuery(sql, new String[]{bushou, start + "", end + ""});
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String zi = cursor.getString(cursor.getColumnIndex("zi"));
            String py = cursor.getString(cursor.getColumnIndex("py"));
            String wubi = cursor.getString(cursor.getColumnIndex("wubi"));
            String pinyin1 = cursor.getString(cursor.getColumnIndex("pinyin"));
            String bushou1 = cursor.getString(cursor.getColumnIndex("bushou"));
            String bihua = cursor.getString(cursor.getColumnIndex("bihua"));
            PinyinWordBean.ResultBean.ListBean bean = new PinyinWordBean.ResultBean.ListBean(id, zi, py, wubi, pinyin1, bushou1, bihua);
            list.add(bean);
        }
        return list;
    }

    public static void insertWordToWordtb(WordBean.ResultBean bean){
        ContentValues values = new ContentValues();
        values.put("id",bean.getId());
        values.put("zi",bean.getZi());
        values.put("py",bean.getPy());
        values.put("wubi",bean.getWubi());
        values.put("pinyin",bean.getPinyin());
        values.put("bushou",bean.getBushou());
        values.put("bihua",bean.getBihua());
        String jijie = listToString(bean.getJijie());
        values.put("jijie",jijie);
        String xiangjie = listToString(bean.getXiangjie());
        values.put("xiangjie",xiangjie);
        db.insert("wordtb",null,values);
    }
    /*
    * 根据输入的汉字，查找到对应的信息所组成的对象
    * */
    public static WordBean.ResultBean queryWordFromWordtb(String word){
        String sql = "select * from wordtb where zi=?";
        Cursor cursor = db.rawQuery(sql, new String[]{word});
        if (cursor.moveToFirst()) {
            String id = cursor.getString(cursor.getColumnIndex("id"));
            String zi = cursor.getString(cursor.getColumnIndex("zi"));
            String py = cursor.getString(cursor.getColumnIndex("py"));
            String wubi = cursor.getString(cursor.getColumnIndex("wubi"));
            String pinyin = cursor.getString(cursor.getColumnIndex("pinyin"));
            String bushou = cursor.getString(cursor.getColumnIndex("bushou"));
            String jijie = cursor.getString(cursor.getColumnIndex("jijie"));
            String xiangjie = cursor.getString(cursor.getColumnIndex("xiangjie"));
            String bihua = cursor.getString(cursor.getColumnIndex("bihua"));
            List<String>jijieList = stringToList(jijie);
            List<String>xxjijieList = stringToList(xiangjie);
            WordBean.ResultBean bean = new WordBean.ResultBean(id, zi, py, wubi, pinyin, bushou, bihua, jijieList, xxjijieList);
            return bean;
        }

        return null;
    }

    public static void insertCyToCyutb(ChengyuBean.ResultBean bean){
        ContentValues values = new ContentValues();
        values.put("chengyu",bean.getChengyu());
        values.put("bushou",bean.getBushou());
        values.put("head",bean.getHead());
        values.put("pinyin",bean.getPinyin());
        values.put("chengyujs",bean.getChengyujs());
        values.put("from_",bean.getFrom_());
        values.put("example",bean.getExample());
        values.put("yufa",bean.getYufa());
        values.put("ciyujs",bean.getCiyujs());
        values.put("yinzhengjs",bean.getYinzhengjs());
        String ty = listToString(bean.getTongyi());
        values.put("tongyi",ty);
        String fy = listToString(bean.getFanyi());
        values.put("fanyi",fy);
        db.insert("cyutb",null,values);
    }

    /* 查询成语表当中所有的记录，只要chengyu这里列*/
    public static List<String>queryAllCyFromCyutb(){
        List<String>cyAllList = new ArrayList<>();
        String sql = "select chengyu from cyutb";
        Cursor cursor = db.rawQuery(sql,null);
        while (cursor.moveToNext()) {
            String chengyu = cursor.getString(cursor.getColumnIndex("chengyu"));
            cyAllList.add(chengyu);
        }
        return cyAllList;
    }
/* 根据传入参数成语，在数据库当中查找这个成语对应的具体内容*/
    public static ChengyuBean.ResultBean queryCyFromCyutb(String cyu){
        String sql = "select * from cyutb where chengyu = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{cyu});
        if (cursor.moveToFirst()) {
            String chengyu = cursor.getString(cursor.getColumnIndex("chengyu"));
            String bushou = cursor.getString(cursor.getColumnIndex("bushou"));
            String head = cursor.getString(cursor.getColumnIndex("head"));
            String pinyin = cursor.getString(cursor.getColumnIndex("pinyin"));
            String chengyujs = cursor.getString(cursor.getColumnIndex("chengyujs"));
            String from_ = cursor.getString(cursor.getColumnIndex("from_"));
            String example = cursor.getString(cursor.getColumnIndex("example"));
            String yufa = cursor.getString(cursor.getColumnIndex("yufa"));
            String ciyujs = cursor.getString(cursor.getColumnIndex("ciyujs"));
            String yinzhengjs = cursor.getString(cursor.getColumnIndex("yinzhengjs"));
            String tongyi = cursor.getString(cursor.getColumnIndex("tongyi"));
            String fanyi = cursor.getString(cursor.getColumnIndex("fanyi"));
            List<String> tyList = stringToList(tongyi);
            List<String> fyList = stringToList(fanyi);
            ChengyuBean.ResultBean bean = new ChengyuBean.ResultBean(chengyu, bushou, head, pinyin,
                    chengyujs, from_, example, yufa, ciyujs, yinzhengjs, tyList, fyList);
            return bean;
        }
        return null;
    }
    public static String listToString(List<String>list){
        StringBuilder sb = new StringBuilder();
        if (list!=null) {
            for (int i = 0; i < list.size(); i++) {
                String msg = list.get(i);
                msg+="|";
                sb.append(msg);
            }
        }
        return sb.toString();
    }

    public static List<String>stringToList(String msg){
        List<String>list = new ArrayList<>();
        if (!TextUtils.isEmpty(msg)) {
            String[] arr = msg.split("\\|");
            for (int i = 0; i < arr.length; i++) {
                String s = arr[i].trim();
                if (!TextUtils.isEmpty(s)) {
                    list.add(s);
                }
            }
        }
       return list;
    }


    /* 向收藏汉字的表格当中插入数据的方法*/
    public static void insertZiToCollwordtb(String zi){
        ContentValues values = new ContentValues();
        values.put("zi",zi);
        db.insert("collwordtb",null,values);
    }

    /* 删除收藏汉字数据的方法*/
    public static void deleteZiToCollwordtb(String zi){
        String sql = "delete from collwordtb where zi = ?";
        db.execSQL(sql,new Object[]{zi});
    }

   /* 查找此汉字是否在收藏表格当中*/
    public static boolean isExistZiInCollwordtb(String zi){
        String sql = "select * from collwordtb where zi = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{zi});
        if (cursor.getCount()>0) {
            return true;
        }
        return false;
    }
    /* 查找汉字收藏表格当中所有的汉字*/
    public static List<String>queryAllZiInCollwordtb(){
        String sql = "select * from collwordtb";
        Cursor cursor = db.rawQuery(sql,null);
        List<String>list = new ArrayList<>();
        while (cursor.moveToNext()) {
            String zi = cursor.getString(cursor.getColumnIndex("zi"));
            list.add(zi);
        }
        return list;
    }

    /* 向收藏成语的表格当中插入数据的方法*/
    public static void insertCyuiToCollcyutb(String cyu){
        ContentValues values = new ContentValues();
        values.put("chengyu",cyu);
        db.insert("collcyutb",null,values);
    }

    /* 删除收藏汉字数据的方法*/
    public static void deleteCyuToCollcyutb(String cyu){
        String sql = "delete from collcyutb where chengyu = ?";
        db.execSQL(sql,new Object[]{cyu});
    }
    /* 查找此成语是否在收藏表格当中*/
    public static boolean isExistCyuInCollcyutb(String cyu){
        String sql = "select * from collcyutb where chengyu = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{cyu});
        if (cursor.getCount()>0) {
            return true;
        }
        return false;
    }

    /* 查找收藏成语表格当中的所有成语*/
    public static List<String>queryAllCyuInCollcyutb(){
        String sql = "select * from collcyutb";
        Cursor cursor = db.rawQuery(sql,null);
        List<String>list = new ArrayList<>();
        while (cursor.moveToNext()) {
            String chengyu = cursor.getString(cursor.getColumnIndex("chengyu"));
            list.add(chengyu);
        }
        return list;
    }
}
